package cn.xd.common.response;

import cn.xd.common.enums.common.ResultCodeEnum;
import cn.xd.common.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author mirzaLin
 * @data 2023/11/13
 * @description 结果类
 **/
@Data
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {
    private String msg;

    private Integer result;

    private T data;

    /**
     * 返回请求-成功
     *
     * @return cn.xd.common.response.ResponseResult<?>
     * @date 2023/11/13
     */

    public static <T> ResponseResult<?> success() {
        return new ResponseResult<>("", ResultCodeEnum.SUCCESS.getResult(), true);
    }

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>("", ResultCodeEnum.SUCCESS.getResult(), data);
    }

    /**
     * 返回请求-尚未登陆或登录过期
     *
     * @param data
     * @return cn.xd.common.response.ResponseResult<T>
     * @date 2023/11/13
     */

    public static <T> ResponseResult<T> unauthorized(T data) {
        return new ResponseResult<>(ResultCodeEnum.UNAUTHORIZED.getInfo(), ResultCodeEnum.UNAUTHORIZED.getResult(), data);
    }

    /**
     * 返回请求-无权限
     *
     * @param data
     * @return cn.xd.common.response.ResponseResult<T>
     * @date 2023/11/13
     */

    public static <T> ResponseResult<T> forbidden(T data) {
        return new ResponseResult<>(ResultCodeEnum.FORBIDDEN.getInfo(), ResultCodeEnum.FORBIDDEN.getResult(), data);
    }

    /**
     * 返回请求-失败
     *
     * @param msg
     * @param ResultCodeEnum
     * @param data
     * @return cn.xd.common.response.ResponseResult<T>
     * @date 2023/11/13
     */
    public static <T> ResponseResult<T> failed(String msg, ResultCodeEnum ResultCodeEnum, T data) {
        return new ResponseResult<>(msg, ResultCodeEnum.getResult(), data);
    }

    public static <T> ResponseResult<T> failed(String msg, Integer result, T data) {
        return new ResponseResult<>(msg, result, data);
    }

    public static <T> ResponseResult<T> failed(String msg, ResultCodeEnum result) {
        return new ResponseResult<>(msg, result.getResult(), null);
    }

    public static <T> ResponseResult<T> failed(String msg) {
        return new ResponseResult<>(msg, ResultCodeEnum.BIZ_ERROR.getResult(), null);
    }

    public static <T> ResponseResult<T> failed(String msg, Integer result) {
        return new ResponseResult<>(msg, result, null);
    }

    /**
     * 获得结果，不成功抛出业务异常
     *
     * @return
     */
    public T get() {
        if (!result.equals(ResultCodeEnum.SUCCESS.getResult())) {
            throw new BizException(msg);
        }
        return data;
    }

    /**
     * 如果不存在抛出异常
     *
     * @param e
     * @date 2023/11/13
     */
    public T ifEmpty(RuntimeException e) {
        if (!result.equals(ResultCodeEnum.SUCCESS.getResult())) {
            throw new BizException(msg);
        }
        if (data == null) {
            throw e;
        } else if (data instanceof Collection && ((Collection<?>) data).isEmpty()) {
            throw e;
        }
        return data;
    }

    /**
     * 如果不存在抛出业务异常
     *
     * @param message
     * @date 2023/11/13
     */
    public T ifEmpty(String message) {
        return ifEmpty(new BizException(message));
    }

    /**
     * 如果存在抛出异常
     *
     * @param e
     * @date 2023/11/13
     */
    public void ifExit(RuntimeException e) {
        if (!result.equals(ResultCodeEnum.SUCCESS.getResult())) {
            throw new BizException(msg);
        }
        if (data != null && !(data instanceof Collection)) {
            throw e;
        } else if (data != null && !((Collection<?>) data).isEmpty()) {
            throw e;
        }

    }

    /**
     * 如果存在抛出业务异常
     *
     * @param message
     * @date 2023/11/13
     */
    public void ifExit(String message) {
        ifExit(new BizException(message));
    }

    /**
     * 校验是否成功
     *
     * @date 2023/11/13
     */
    public void valid() {
        if (!result.equals(ResultCodeEnum.SUCCESS.getResult())) {
            throw new BizException(msg);
        }
    }
}
